#! /bin/sh
# OpenSearchServer start
#

### BEGIN INIT INFO
# Provides:          opensearchserver
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start/stop OpenSearchServer at boot time
# Description:       Enable service provided by OpenSearchServer.
### END INIT INFO

CONFIGFILE=/etc/opensearchserver
if [ -f "$CONFIGFILE" ]; then
    . "$CONFIGFILE"
fi

export OPENSEARCHSERVER_DATA
export OPENSEARCHSERVER_MULTIDATA
export OPENSEARCHSERVER_DEBUG

OPENSEARCHSERVER_DIR=/var/lib/opensearchserver
OPENSEARCHSERVER_SHARE=/usr/share/opensearchserver
OPENSEARCHSERVER_JAR=$OPENSEARCHSERVER_SHARE/opensearchserver.jar
SERVER_DIR=$OPENSEARCHSERVER_DIR/server
SERVER_OPTS="$JAVA_OPTS -Djava.protocol.handler.pkgs=jcifs -Dfile.encoding=UTF-8 -jar $OPENSEARCHSERVER_JAR -extractDirectory $SERVER_DIR -httpPort $SERVER_PORT -uriEncoding UTF-8"
SERVER_LOG=/var/log/opensearchserver/server.out
SERVER_PID="/var/run/opensearchserver.pid"
export SERVER_USER=opensearchserver

usage ()
{
  echo "Usage : $0 {start|stop|restart}"
  exit
}

if [ "$#" -ne 1 ]
then
  usage
fi

case $1 in
start)
        if [ -f $SERVER_PID ]; then
            echo "OpenSearchServer is already running ..."
        else
            echo "Starting OpenSearchServer ..."
            PID=`su -c "/usr/bin/java $SERVER_OPTS" $SERVER_USER >> $SERVER_LOG 2>&1 & echo $!`
            if [ -z $PID ]; then
               echo "OpenSearchServer starting failed"
            else
               echo $PID > $SERVER_PID
               echo "OpenSearchServer started ..."
            fi
        fi
        ;;
stop)
        if [ -f $SERVER_PID ]; then
            echo "Stopping OpenSearchServer ..."
            PID=`cat $SERVER_PID`
            kill $PID
            echo "OpenSearchServer stopped ..."
            rm $SERVER_PID
        else
            echo "OpenSearchServer is not running ..."
        fi
        ;;
restart|force-reload)
        $0 stop
        $0 start
		;;
esac
exit 0
